package com.yili.oa.dao.extend;

import com.yili.oa.entity.FtAccountDetailsSearch;
import com.yili.oa.utils.ListUtils;
import org.apache.ibatis.annotations.Param;

import java.util.List;


public class FtAccountDetailsExtendSqlProvider {

    public String get(FtAccountDetailsSearch searchBase){
        StringBuffer sb = new StringBuffer();
        String sql = "SELECT a.id,FROM_UNIXTIME(trade_time/1000) as trade_time,a.expense,a.income,a.account_balance,a.trade_type,a.user_id,b.trade_type as tradeType from (select * from ft_account_details where 1=1  ";
        sb.append(sql);
        if(ListUtils.isNotNullOrEmpty(searchBase.getIds())){
            List<Long> ids = searchBase.getIds();
            sb.append(" and user_id in (");
            for (int i = 0; i < ids.size(); i++) {
                if(i!=ids.size()-1){
                    sb.append(ids.get(i)+",");
                }else{
                    sb.append(ids.get(i)+")");
                }
            }

        }
        if(searchBase.getTradeType()!=null){
            sb.append(" and trade_type =" + searchBase.getTradeType());
        }
        if(searchBase.getBegin()!=null){
            sb.append(" and trade_time >=" + searchBase.getBegin());
        }
        if (searchBase.getEnd()!= null) {
            sb.append(" and trade_time <=" + searchBase.getEnd());
        }
        sb.append(" )as a left join ft_trade_type as b on a.trade_type = b.id ");
        if(searchBase.getOrderBy()!=null){
            sb.append(" ORDER BY "+searchBase.getOrderBy());
        }else{
            sb.append(" ORDER BY trade_time desc ");
        }
        return sb.toString();

    }

    public String getSum(@Param("direction")String direction, @Param("userId")Long userId,@Param("tradeTypes") List<Integer> tradeTypes){
        StringBuffer sb = new StringBuffer();
        String sql = "select sum("+direction+") as amount from ft_account_details where user_id="+userId;
        sb.append(sql);
        if(ListUtils.isNotNullOrEmpty(tradeTypes)){
            sb.append(" and trade_type in (");
            for (int i = 0; i < tradeTypes.size(); i++) {
                if(i!=tradeTypes.size()-1){
                    sb.append(tradeTypes.get(i)+",");
                }else{
                    sb.append(tradeTypes.get(i)+")");
                }
            }

        }
        return sb.toString();
    }


}


